package com.example.demo.controller;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int L = 0, R = 0;
        int[] numArr = new int[3];
        int i = 0;
        while(i < 3){ //判断是否结束
            if(L == 0){
                L = sc.nextInt();//读入整数
            }else if(R == 0){
                R = sc.nextInt();
            }
            if(L != 0 && R != 0){
                numArr[i++] = sc.nextInt();
            }
        }
        System.out.println(String.valueOf(fun(L,R,numArr[0],numArr[1],numArr[2])));
    }
    public static int fun(int L, int R, int p,int l, int r){
        int count = 0;
        int L_dec = binaryToDecimal(L);
        int R_dec = binaryToDecimal(R);
//        int p = 7, l = 0, r = 4;
        for(int i = L_dec; i <= R_dec; i++){
            if(popcount(i) == 3 && i % p >= l && i % p <= r){
                count++;
            }
        }
        return count;
    }
    public static int binaryToDecimal(int n){
        int decimal = 0, p = 0;
        while(n!=0)
        {
            decimal+=((n%10)*Math.pow(2,p));
            n=n/10;
            p++;
        }
        return decimal;
    }
    public static int popcount(int n){
        int count_1 = 0;
        while(n != 0){
            if(n % 2 != 0){
                count_1++;
            }
            n /= 2;
        }
        return count_1;
    }
}
